package com.tsd.utils;

import com.tsd.core.constants.Constants;
import com.tsd.core.utils.DateUtil;
import com.tsd.core.utils.FieldUtils;
import com.tsd.core.utils.HlpUtils;
import com.tsd.system.entity.SysUser;

import java.util.Date;

/**
 * @ClassName: AutoInjectUtils
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2024/4/15 18:43
 * @Version: 1.0
 */
public class AutoInjectUtils {

    public static final String TYPE_CREATE = "create";
    public static final String TYPE_UPDATE = "update";
    public static final String TYPE_DELETE = "delete";
    public static final String TYPE_AUDIT = "audit";
    public static final String TYPE_UPDATE_STOCK = "update_stock";


    /**
     * 自动注入数据
     *
     * @param object 对象
     * @param opUser 登录用户
     * @param type   操作数据类型：TYPE_CREATE(新增)、TYPE_UPDATE(修改)、TYPE_DELETE(删除)、TYPE_AUDIT(审核)
     */
    public static void autoInjectBaseData(Object object, SysUser opUser, String type) {
        Date today = new Date();
        autoInjectBaseData(object, opUser, type, today, today);
    }

    /**
     * 自动注入数据
     *
     * @param object   对象
     * @param opUser   登录用户
     * @param type     操作数据类型：TYPE_CREATE(新增)、TYPE_UPDATE(修改)、TYPE_DELETE(删除)
     * @param today
     * @param yyyyMMdd
     */
    public static void autoInjectBaseData(Object object, SysUser opUser, String type, Date today, Date yyyyMMdd) {
        if (today == null) {
            today = new Date();
        }
        if (TYPE_CREATE.equals(type)) {
            if (yyyyMMdd == null) {
                yyyyMMdd = today;
            }
            FieldUtils.setFieldValue(object, "sid", HlpUtils.getUUIDWithout_(), true);
            FieldUtils.setFieldValue(object, "yyyy", DateUtil.getYyyy(yyyyMMdd), false);
            FieldUtils.setFieldValue(object, "mm", DateUtil.getMm(yyyyMMdd), false);
            FieldUtils.setFieldValue(object, "dd", DateUtil.getDd(yyyyMMdd), false);
            FieldUtils.setFieldValue(object, "ten_days", DateUtil.getTenDay(yyyyMMdd), false);
            FieldUtils.setFieldValue(object, "time_tag", DateUtil.getTimeTag(yyyyMMdd), false);
            FieldUtils.setFieldValue(object, "sn", 99999, false);
            FieldUtils.setFieldValue(object, "state", 1, false);
            FieldUtils.setFieldValue(object, "create_date", today, true);
            FieldUtils.setFieldValue(object, "createDate", today, true);
            FieldUtils.setFieldValue(object, "creator_sid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "creator_name", opUser == null ? null : opUser.getName(), true);
            FieldUtils.setFieldValue(object, "creatorSid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "creatorName", opUser == null ? null : opUser.getName(), true);
            FieldUtils.setFieldValue(object, "group_sid", opUser == null ? null : opUser.getGroup_sid(), false);
            FieldUtils.setFieldValue(object, "groupSid", opUser == null ? null : opUser.getGroup_sid(), false);
            FieldUtils.setFieldValue(object, "dr", 0, false);
        } else if (TYPE_UPDATE.equals(type)) {
            FieldUtils.setFieldValue(object, "update_date", today, true);
            FieldUtils.setFieldValue(object, "updator_sid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "updator_name", opUser == null ? null : opUser.getName(), true);
            FieldUtils.setFieldValue(object, "updateDate", today, true);
            FieldUtils.setFieldValue(object, "updatorSid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "updatorName", opUser == null ? null : opUser.getName(), true);
        } else if (TYPE_DELETE.equals(type)) {
            FieldUtils.setFieldValue(object, "dr", 1, true);
            FieldUtils.setFieldValue(object, "dr_date", today, true);
            FieldUtils.setFieldValue(object, "dr_user_sid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "dr_user_name", opUser == null ? null : opUser.getName(), true);
            FieldUtils.setFieldValue(object, "drDate", today, true);
            FieldUtils.setFieldValue(object, "drUserSid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "drUserName", opUser == null ? null : opUser.getName(), true);
        } else if (TYPE_AUDIT.equals(type)) {
            FieldUtils.setFieldValue(object, "audit_date", today, true);
            FieldUtils.setFieldValue(object, "auditor_sid", opUser == null ? null : opUser.getSid(), true);
            FieldUtils.setFieldValue(object, "auditor_name", opUser == null ? null : opUser.getName(), true);
        } else if (TYPE_UPDATE_STOCK.equals(type)) {
            FieldUtils.setFieldValue(object, "update_stock", 1, true);
            FieldUtils.setFieldValue(object, "update_stock_date", new Date(), true);
        }
    }

    /**
     * 更新对象的yyyy\mm\dd的值
     *
     * @param object
     * @param yyyyMMdd
     */
    public static void updateYyyyMmdd(Object object, Date yyyyMMdd) {
        if (yyyyMMdd != null && object != null) {
            FieldUtils.setFieldValue(object, "yyyy", DateUtil.getYyyy(yyyyMMdd), true);
            FieldUtils.setFieldValue(object, "mm", DateUtil.getMm(yyyyMMdd), true);
            FieldUtils.setFieldValue(object, "dd", DateUtil.getDd(yyyyMMdd), true);
        }
    }

    /**
     * 清除数据
     *
     * @param object 对象
     */
    public static void clearBaseData(Object object) {
        FieldUtils.setFieldValue(object, "id", null, true);
        FieldUtils.setFieldValue(object, "sid", null, true);
        clearBaseDataWithIdSid(object);
    }

    public static void clearBaseDataWithIdSid(Object object) {
        FieldUtils.setFieldValue(object, "yyyy", null, true);
        FieldUtils.setFieldValue(object, "mm", null, true);
        FieldUtils.setFieldValue(object, "dd", null, true);
        FieldUtils.setFieldValue(object, "ten_days", null, true);
        FieldUtils.setFieldValue(object, "time_tag", null, true);
        FieldUtils.setFieldValue(object, "sn", null, true);
        FieldUtils.setFieldValue(object, "state", null, true);
        FieldUtils.setFieldValue(object, "create_date", null, true);
        FieldUtils.setFieldValue(object, "creator_sid", null, true);
        FieldUtils.setFieldValue(object, "creator_name", null, true);
        FieldUtils.setFieldValue(object, "group_sid", null, true);
        FieldUtils.setFieldValue(object, "update_date", null, true);
        FieldUtils.setFieldValue(object, "updator_sid", null, true);
        FieldUtils.setFieldValue(object, "updator_name", null, true);
        FieldUtils.setFieldValue(object, "dr", Constants.VALUE_NO, true);
        FieldUtils.setFieldValue(object, "dr_date", null, true);
        FieldUtils.setFieldValue(object, "dr_user_sid", null, true);
        FieldUtils.setFieldValue(object, "dr_user_name", null, true);
        FieldUtils.setFieldValue(object, "file", null, true);
    }
}
